package allen.util;

import java.util.Random;

public class RandomUtil {
	private static Random r = new Random();

	/**
	 * return a random number between min and max(both inclusive).
	 * 
	 * @param min
	 *            min<=max.
	 */
	public static int getRandomNum(int min, int max) {
		if (max < min)
			throw new IllegalArgumentException(String.format(
					"min(%1$d)>max(%2$d).", min, max));

		int rnum = r.nextInt(max - min + 1);

		return min + rnum;
	}

	/**
	 * return a random boolean.
	 * 
	 * @param d
	 *            0<=d<=1
	 */
	public static boolean getRandomBoolean(double d) {
		if (d < 0 || d > 1)
			throw new IllegalArgumentException(String.format("d=%1$d.", d));
		return r.nextDouble() < d;
	}

}
